

package com.hazelcast.partition.strategy;

import com.hazelcast.internal.serialization.SerializableByConvention;
import com.hazelcast.partition.PartitioningStrategy;

@SerializableByConvention
@SuppressWarnings("JavadocVariable")
public class StringPartitioningStrategy implements PartitioningStrategy {

    public static final StringPartitioningStrategy INSTANCE = new StringPartitioningStrategy();

    public static String getBaseName(String name) {
        if (name == null) {
            return null;
        }
        int indexOf = name.indexOf('@');
        if (indexOf == -1) {
            return name;
        }
        return name.substring(0, indexOf);
    }

    public static String getPartitionKey(String key) {
        if (key == null) {
            return null;
        }

        int firstIndexOf = key.indexOf('@');
        if (firstIndexOf == -1) {
            return key;
        } else {
            return key.substring(firstIndexOf + 1);
        }
    }

    @Override
    public Object getPartitionKey(Object key) {
        if (key instanceof String string) {
            return getPartitionKey(string);
        }
        return null;
    }
}
